package prepare.huawei;

import java.util.Scanner;

/**
 * description: <a href="https://blog.csdn.net/weixin_41934659/article/details/125746558">可以组成网络的服务器</a>
 *
 * @author <a href="zhaoyingling12@163.com">simon zhao</a>
 * @since 2023/3/23
 */
public class ServersFormNetwork {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String[] nm = input.nextLine().split(" ");
        int n = Integer.parseInt(nm[0]), m = Integer.parseInt(nm[1]);
        int[][] servers = new int[n][m];
        int index = 0;
        // 获取输入数据
        while (index < n) {
            String[] ss = input.nextLine().split(" ");
            for (int j = 0; j < ss.length; j++) {
                servers[index][j] = Integer.parseInt(ss[j]);
            }
            index++;
        }
        boolean[][] visited = new boolean[m][n];
        int maxServers = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (visited[i][j]) {
                    continue;
                }
                if (servers[i][j] != 1) {
                    continue;
                }
                visited[i][j] = true;
                int cnt = dfs(servers, visited, i, j, 1);
                maxServers = Math.max(cnt, maxServers);
            }
        }
        System.out.println(maxServers);
    }

    private static int dfs(int[][] servers, boolean[][] visited, int x, int y, int cnt) {

        // 向上
        int upCnt = 0;
        if (x - 1 >= 0 && !visited[x - 1][y] && servers[x - 1][y] == 1) {
            visited[x - 1][y] = true;
            upCnt = dfs(servers, visited, x - 1, y, 1);
        }

        // 向下
        int downCnt = 0;
        if (x + 1 < servers.length && !visited[x + 1][y] && servers[x + 1][y] == 1) {
            visited[x + 1][y] = true;
            downCnt = dfs(servers, visited, x + 1, y, 1);
        }

        // 向左
        int leftCnt = 0;
        if (y - 1 > 0 && !visited[x][y - 1] && servers[x][y - 1] == 1) {
            visited[x][y - 1] = true;
            leftCnt = dfs(servers, visited, x, y - 1, 1);
        }

        // 向右
        int rightCnt = 0;
        if (y + 1 < servers[0].length && !visited[x][y + 1] && servers[x][y + 1] == 1) {
            visited[x][y + 1] = true;
            rightCnt = dfs(servers, visited, x, y + 1, 1);
        }
        return cnt + upCnt + downCnt + leftCnt + rightCnt;
    }
}
